﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 智信科技物流系统
{
    public partial class Search : Form
    {
        public Search()
        {
            InitializeComponent();
        }



        //private void AddSource(ListBox lst)
        //{
        //    Area area = new Area();
        //    List<Area> list=new List<Area>();
        //    for (int i = 0; i < lst.Items.Count; i++)
        //    {
        //        area.AreaName=lst.Items[i].ToString();
        //        list.Add(area);
        //    }
        //}

        int AddLinkage(ListBox lst, ListBox plst)
        {
            try
            {
                SqlHelper helper = new SqlHelper();
                string provence = lst.SelectedValue.ToString().Trim();
                int id = helper.SqlScalar("select AreaId from AreaFull where AreaName like '%" + provence.Trim() + "%' ");
                DataTable dt = helper.SqlSelect("select AreaName from AreaFull where AreaPid=" + id);
                plst.DataSource = dt;
                plst.DisplayMember = dt.Columns[0].ColumnName;
                plst.ValueMember = dt.Columns[0].ColumnName;



                return 1;

            }
            catch
            {
                MessageBox.Show("数据加载错误或操作错误，请重试");
                return 0;
            }
        }


        private void MoveItems(ListBox lstfrom, ListBox lstget)
        {
            Area area = new Area();
            List<Area> list = new List<Area>();
            for (int i = 0; i < lstfrom.Items.Count; i++)
            {
                area.AreaName = lstfrom.Items[i].ToString();
                list.Add(area);
                lstget.Items.Add(list[i].AreaName.ToString());

            }
        }






        private void Search_Load(object sender, EventArgs e)
        {
            SqlHelper helper = new SqlHelper();
            DataTable dt = helper.SqlSelect("select AreaName from AreaFull where Areapid=0 ");
            lst_StartProvence.DataSource = dt;
            lst_StartProvence.DisplayMember = dt.Columns[0].ColumnName;
            lst_StartProvence.ValueMember = dt.Columns[0].ColumnName;
            lst_EndProvence.DataSource = dt;
            lst_EndProvence.DisplayMember = dt.Columns[0].ColumnName;
            lst_EndProvence.ValueMember = dt.Columns[0].ColumnName;

            lst_StartAllProvence.DataSource = dt;
            lst_StartAllProvence.DisplayMember = dt.Columns[0].ColumnName;
            lst_StartAllProvence.ValueMember = dt.Columns[0].ColumnName;
            lst_EndAllProvence.DataSource = dt;
            lst_EndAllProvence.DisplayMember = dt.Columns[0].ColumnName;
            lst_EndAllProvence.ValueMember = dt.Columns[0].ColumnName;



        }



        private void lst_StartProvence_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_StartProvence, lst_StartCity);


        }



        private void lst_StartCity_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_StartCity, lst_StartCounty);

        }

        private void lst_StartCounty_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_StartKey.Items.Add(lst_StartCounty.SelectedValue);
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            if (tb_StartSearchKey.Text.Trim() == null || tb_StartSearchKey.Text.Trim() == "")
            {
                MessageBox.Show("关键字不能为空");
                return;
            }
            if (tb_StartSearchKey.Text.Trim() == null || tb_StartSearchKey.Text.Trim() == "")
            {
                MessageBox.Show("关键字不能为空");
                return;
            }
            if (lst_StartKey.Items.Count == 0)
            {
                MessageBox.Show("请选择搜索城市");
                return;
            }
            SearchClass searchclass = new SearchClass();

            string quanju = "";
            for (int i = 0; i < lst_StartKey.Items.Count - 1; i++)
            {
                quanju = quanju + lst_StartKey.Items[i] + ";";
            }
            quanju = quanju + lst_StartKey.Items[lst_StartKey.Items.Count - 1];

            string sql;
            string add = searchclass.TrimStr(quanju, "DepartureCity");
            string key = searchclass.TrimStr(tb_StartSearchKey.Text.ToString().Trim(), "InformationContent");
            if (add == null || key == null)
            {
                return;
            }
            sql = searchclass.Package(add, key);
            SqlHelper helper = new SqlHelper();


            //重写的搜索结果代码
            List<oneLogistics> result = new List<oneLogistics>();
            Dao dao = new Dao();

            View frm_result = new View();
            frm_result.dg_view.Columns.Add("trueid", "trueid");
            frm_result.dg_view.Columns["trueid"].Visible = false;
            frm_result.dg_view.Columns.Add("id", "id");
            frm_result.dg_view.Columns.Add("DepartureCity", "DepartureCity");
            frm_result.dg_view.Columns.Add("content", "content");
            frm_result.dg_view.Columns["content"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            frm_result.Text = "搜索结果";
            frm_result.Width = this.Width;
            frm_result.Height = this.Height;
            frm_result.Location = new Point(this.Location.X, this.Location.Y );

            dao.GetLogistics(result, sql);
            dao.BindDataGridView(frm_result.dg_view, result);

            frm_result.ShowDialog();
            //end


            /*之前的
            Area.dt = helper.SqlSelect(sql);
          SearchResult searchresult = new SearchResult();
          searchresult.ShowDialog();
            */
        }

        private void gb_SearchSize_Enter(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void btn_EndSearch_Click(object sender, EventArgs e)
        {
            if (tb_EndSearchKey.Text.Trim() == null || tb_EndSearchKey.Text.Trim() == "")
            {
                MessageBox.Show("关键字不能为空");
                return;
            }
            if (tb_EndSearchKey.Text.Trim() == null || tb_EndSearchKey.Text.Trim() == "")
            {
                MessageBox.Show("关键字不能为空");
                return;
            }
            if (lst_EndKey.Items.Count == 0)
            {
                MessageBox.Show("请选择搜索城市");
                return;
            }
            SearchClass searchclass = new SearchClass();

            string quanju = "";
            for (int i = 0; i < lst_EndKey.Items.Count - 1; i++)
            {
                quanju = quanju + lst_EndKey.Items[i] + ";";
            }
            quanju = quanju + lst_EndKey.Items[lst_EndKey.Items.Count - 1];

            string sql;
            string add = searchclass.TrimStr(quanju, "ReachCity");
            string key = searchclass.TrimStr(tb_EndSearchKey.Text.ToString().Trim(), "InformationContent");
            if (add == null || key == null)
            {
                return;
            }
            sql = searchclass.Package(add, key);

            //重写的搜索结果代码
            List<oneLogistics> result = new List<oneLogistics>();
            Dao dao = new Dao();

            View frm_result = new View();
            frm_result.dg_view.Columns.Add("trueid", "trueid");
            frm_result.dg_view.Columns["trueid"].Visible = false;
            frm_result.dg_view.Columns.Add("id", "id");
            frm_result.dg_view.Columns.Add("DepartureCity", "DepartureCity");
            frm_result.dg_view.Columns.Add("content", "content");
            frm_result.dg_view.Columns["content"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            frm_result.Text = "搜索结果";
            frm_result.Width = this.Width;
            frm_result.Height = this.Height;
            frm_result.Location = new Point(this.Location.X, this.Location.Y);

            dao.GetLogistics(result, sql);
            dao.BindDataGridView(frm_result.dg_view, result);

            frm_result.ShowDialog();
            //end
        }

        private void lst_EndProvence_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_EndProvence, lst_EndCity);
        }

        private void lst_EndCity_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_EndCity, lst_EndCounty);
        }

        private void lst_EndCounty_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_EndKey.Items.Add(lst_EndCounty.SelectedValue);
        }

        private void lst_StartAllProvence_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_StartAllProvence, lst_StartAllCity);
        }

        private void lst_StartAllCity_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_StartAllCity, lst_StartAllCounty);
        }

        private void lst_StartAllCounty_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_StartAllKey.Items.Add(lst_StartAllCounty.SelectedValue);
        }

        private void lst_EndAllProvence_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_EndAllProvence, lst_EndAllCity);
        }

        private void lst_EndAllCity_MouseClick(object sender, MouseEventArgs e)
        {
            AddLinkage(lst_EndAllCity, lst_EndAllCounty);
        }

        private void lst_EndAllCounty_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_EndAllKey.Items.Add(lst_EndAllCounty.SelectedValue);
        }

        private void btn_AllSearch_Click(object sender, EventArgs e)
        {
            if (tb_AllSearchKey.Text.Trim() == null || tb_AllSearchKey.Text.Trim() == "")
            {
                MessageBox.Show("关键字不能为空");
                return;
            }

            if (lst_EndAllKey.Items.Count == 0 || lst_StartAllKey.Items.Count == 0)
            {
                MessageBox.Show("请选择搜索城市");
                return;
            }
            SearchClass searchclass = new SearchClass();
            string quanjustart = "";
            string quanjuend = "";

            for (int i = 0; i < lst_EndAllKey.Items.Count - 1; i++)
            {
                quanjuend = quanjuend + lst_EndAllKey.Items[i] + ";";
            }
            quanjuend = quanjuend + lst_EndAllKey.Items[lst_EndAllKey.Items.Count - 1];


            for (int i = 0; i < lst_StartAllKey.Items.Count - 1; i++)
            {
                quanjustart = quanjustart + lst_StartAllKey.Items[i] + ";";
            }
            quanjustart = quanjustart + lst_StartAllKey.Items[lst_StartAllKey.Items.Count - 1];


            string sql;
            string addstart = searchclass.TrimStr(quanjustart, "DepartureCity");
            string addend = searchclass.TrimStr(quanjuend, "ReachCity");
            string key = searchclass.TrimStr(tb_AllSearchKey.Text.ToString().Trim(), "InformationContent");
            if (addstart == null || key == null || addend == null)
            {
                return;
            }
            sql = searchclass.Package(addstart, addend, key);

            //重写的搜索结果代码
            List<oneLogistics> result = new List<oneLogistics>();
            Dao dao = new Dao();

            View frm_result = new View();
            frm_result.dg_view.Columns.Add("trueid", "trueid");
            frm_result.dg_view.Columns["trueid"].Visible = false;
            frm_result.dg_view.Columns.Add("id", "id");
            frm_result.dg_view.Columns.Add("DepartureCity", "DepartureCity");
            frm_result.dg_view.Columns.Add("content", "content");
            frm_result.dg_view.Columns["content"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            frm_result.Text = "搜索结果";
            frm_result.Width = this.Width;
            frm_result.Height = this.Height;
            frm_result.Location = new Point(this.Location.X, this.Location.Y);

            dao.GetLogistics(result, sql);
            dao.BindDataGridView(frm_result.dg_view, result);

            frm_result.ShowDialog();
            //end
        }

        private void lst_StartProvence_DrawItem(object sender, DrawItemEventArgs e)
        {
            //DataRowView drs = this.lst_StartProvence.Items[e.Index] as DataRowView;  // 获取Listbox 项的集合  
            //// 选中项的颜色----------------------------------------
            //if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            //{
            //    e.Graphics.FillRectangle(Brushes.Green, e.Bounds);
            //}
            //else
            //{
            //    e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            //}
            //// 选中项的颜色--------------------------------------------

            //// ------------- 奇偶行的背景颜色
            ////if (e.Index % 2 == 0)
            ////{
            ////    e.Graphics.FillRectangle(Brushes.Green, e.Bounds);
            ////}
            ////if (drs[listBox1.DisplayMember].ToString()=="admin")        // 获取项的值
            ////{
            ////    e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            ////}
            ////else
            ////{
            ////    e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
            ////}
            ////   --------------------------------奇偶行的背景颜色

            //e.Graphics.DrawString(drs[lst_StartProvence.DisplayMember].ToString(), this.Font, Brushes.AliceBlue, e.Bounds);   // 这是必须添加的，否则项不显示
        }

        private void lst_StartProvence_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_StartKey.Items.Add(lst_StartProvence.SelectedValue);
        }

        private void lst_StartCity_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_StartKey.Items.Add(lst_StartCity.SelectedValue);
        }

        private void lst_EndProvence_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_EndKey.Items.Add(lst_EndProvence.SelectedValue);
        }

        private void lst_EndCity_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_EndKey.Items.Add(lst_EndCity.SelectedValue);
        }

        private void lst_StartAllProvence_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_StartAllKey.Items.Add(lst_StartAllProvence.SelectedValue);
        }

        private void lst_StartAllCity_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_StartAllKey.Items.Add(lst_StartAllCity.SelectedValue);
        }

        private void lst_EndAllProvence_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_EndAllKey.Items.Add(lst_EndAllProvence.SelectedValue);
        }

        private void lst_EndAllCity_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lst_EndAllKey.Items.Add(lst_EndAllCity.SelectedValue);
        }

        private void btn_StartAddCity_Click(object sender, EventArgs e)
        {
            MoveItems(lst_StartCity, lst_StartKey);
        }

        private void btn_StartAddCounty_Click(object sender, EventArgs e)
        {
            MoveItems(lst_StartCounty, lst_StartKey);
        }

        private void btn_EndAddCity_Click(object sender, EventArgs e)
        {
            MoveItems(lst_EndCity, lst_EndKey);
        }

        private void btn_EndAddCounty_Click(object sender, EventArgs e)
        {
            MoveItems(lst_EndCounty, lst_EndKey);
        }

        private void btn_StartAllAddCity_Click(object sender, EventArgs e)
        {
            MoveItems(lst_StartAllCity, lst_StartAllKey);
        }

        private void btn_StartAllAddCounty_Click(object sender, EventArgs e)
        {
            MoveItems(lst_StartAllCounty, lst_StartAllKey);
        }

        private void btn_EndAllAddCity_Click(object sender, EventArgs e)
        {
            MoveItems(lst_EndAllCity, lst_EndAllKey);
        }

        private void btn_EndAllAddCounty_Click(object sender, EventArgs e)
        {
            MoveItems(lst_EndAllCounty, lst_EndAllKey);
        }

        private void btn_StartAddDelete_Click(object sender, EventArgs e)
        {
            lst_StartAllKey.Items.Remove(lst_StartAllKey.SelectedItem);
        }

        private void btn_EndAddDelete_Click(object sender, EventArgs e)
        {
            lst_EndAllKey.Items.Remove(lst_EndAllKey.SelectedItem);
        }

        private void btn_EndDelete_Click(object sender, EventArgs e)
        {
            lst_EndKey.Items.Remove(lst_EndKey.SelectedItem);
        }

        private void btn_StartDelete_Click(object sender, EventArgs e)
        {
            lst_StartKey.Items.Remove(lst_StartKey.SelectedItem);
        }

        private void btn_StartClear_Click(object sender, EventArgs e)
        {
            lst_StartKey.Items.Clear();
        }

        private void btn_EndClear_Click(object sender, EventArgs e)
        {
            lst_EndKey.Items.Clear();
        }

        private void btn_StartAllClear_Click(object sender, EventArgs e)
        {
            lst_StartAllKey.Items.Clear();
        }

        private void btn_EndAllClear_Click(object sender, EventArgs e)
        {
            lst_EndAllKey.Items.Clear();
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }
    }
}
